<template>
    <!-- 详情 -->
    <el-dialog title="会员充值" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template>
                <el-col :span="24">
                    <el-form-item label="充值金额" prop="amount">
                        <c-money-input v-model="ruleForm.amount" placeholder="请输入充值金额" @change="onAmount"><template slot="append"> 元 </template></c-money-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="会员等级">
                        <el-input :disabled="true" :value="ruleForm.grade"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="优惠比例">
                        <el-input :disabled="true" :value="ruleForm.discountRatio"><template slot="append"> % </template></el-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    data () {
        return {
            dialogVisible: false,
            loading: false,
            ruleForm: {},
            rules: {
                amount: [
                    { required: true, message: "请输入充值金额", trigger: "blur" },
                ],
            },
        };
    },
    methods: {
        onAmount (value) {
            const params = {
                rechargeAmount: value,
            };
            const url = "/MemberInfo/getMemberInfo";
            this.$get({ url, params }).then((res) => {
                if (res) {
                    this.$set(this.ruleForm, "discountRatio", res.discountRatio);
                    this.$set(this.ruleForm, "grade", res.grade);
                    this.$set(this.ruleForm, "memberId", res.id);
                } else {
                    this.$set(this.ruleForm, "discountRatio", "");
                    this.$set(this.ruleForm, "grade", "");
                    this.$set(this.ruleForm, "memberId", "");
                }
            });
        },
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认充值此金额?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const params = JSON.parse(JSON.stringify(this.ruleForm));
                        console.log(params);
                        this.$post({ url: `/sellerAmount/amountIn`, params }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {

            if (item && item.id) {
                console.log(item);
                this.ruleForm = {
                    accountId: item.id
                };
            }
            this.dialogVisible = true;
        },
        handleClose () {
            this.ruleForm = {};
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
